          PH.ARGS DRPT,SDT,EDT,BRCHS,BR,BANK.ID,VOID,SORTOPT,GTYP,OPT,FITEM
** Version# 25.0002[13] - 01/18/2017 - 03:12pm - TSMITH - eclipse
*** V25.0002 Change - Custom Coding CUSTOM - 01/18/2017 - TSMITH - eclipse
** Copied from BP CD.PHR.CHECK.REG Version# 25 - 08/31/2012 - 11:30am - EILEENM - main

*** Subroutine: CD.PHR.CHECK.REG
*-------------------------------------------------------------------------*
*** This routine prints out a register of all the checks during the two
*** dates entered by the user for a specific account and branch.
*-------------------------------------------------------------------------*
*** DRPT    - Report ID                                              (OUT)
*** SDT     - Start Date to check Posting Date with.                 (IN)
*** EDT     - End Date to check Posting Date with.                   (IN)
*** BRCHS   - Branches to check.                                     (IN)
*** BANK.ID - Bank ID that check was cut from.                       (IN)
*** VOID    - Flag to show voided checks or not.                     (IN)
*** SORTOPT - Sort Options - Check or Vendor                         (IN)
*** GTYP    - Manual, Computer or Both types of checks               (IN)
*** OPT     - Detail or Summary                                      (IN)
*** FITEM   - Flag to show voided checks or not.                     (IN)
*-------------------------------------------------------------------------*
*** COMMON VARIABLES : None
*-------------------------------------------------------------------------*
          TGT      = ''
          CURR.FLG = NO
          BASE.CUR = YES
          IF DRPT<51> THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF DRPT<51> # BASEC THEN
                TGT = DRPT<51>
                CURR.FLG = YES
                BASE.CUR = NO
             END
          END

          *** Define starting and ending dates to their respective
          *** variables to use throughout this routine
          SD        = OCONV(SDT,'D2/')
          ED        = OCONV(EDT,'D2/')

          BRS  = BR

          *** Get the name of the bank account
          BANK.NAME = OCONV(BANK.ID,'TGENLED;X;3;3')
          IF FITEM = '' THEN FILTER.FLAG = NO ELSE FILTER.FLAG = YES

          WRITE 'Selecting...' ON PHSTFILE,PID$

          *** Select all checks for the account entered by the user that
          *** meet the date criteria user entered
          GOSUB SEL.IDS
*-------------------------------------------------------------------------*
          WRITE 'Spooling...' ON PHSTFILE,PID$

          *** Define the heading for the report
          HDG = 'AD Check Register for ':SD:' to ':ED:' - Option : ':GTYP:'                                                            Page : ^####'
          BRANCH.ALLOW = 129
          IF LEN(BRS) > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'

          *** Defing the name of the columns for this report
          HDG<1,2> = 'Branches:  ':BRS
          HDG<1,3> = 'Bank Account:  ':BANK.NAME
          IF CURR.FLG THEN HDG<1,3> := '   -    Report Currency:  ':TGT

          IF OPT = "Detail" THEN
             HDG<1,4> = 'Check#.. CD Br#  Chk Date   Check Amt   Vendor.....................  Invoice #......  Inv Date  Gross Amt.  Deductions  Discount..'
          END ELSE
             HDG<1,4> = 'Check#.. CD Br#  Chk Date   Check Amt'
          END

          *** Define any variables needed throughout this routine
          LAST.VEN    = '@@@'
          GTOL.CHECK  = 0              ;* Check Amt Total
          GTOL.GROSS  = 0              ;* Gross Amt Total
          GTOL.DEDUCT = 0              ;* Deduction Amt Total
          GTOL.DISC   = 0              ;* Discount Amt Total

          IF DRPT<33> = '' THEN
             TITLE = 'AD Check Register ':SD:' - ':ED
          END ELSE
             TITLE = DRPT<33>:' ':SD:' - ':ED
          END

          PRINTER.ON 140,'AD Check Register',DOC.ID,HDG,RPT.DFLT=DRPT
          FILTER.PRINT 'Y',FITEM

          *** Get data for each of the checks that will be included in the
          *** report
          CD.CT = DCOUNT(CD.IDS,AM)
          FOR CDN=1 TO CD.CT
             *** Get all the data for the check from the list created with
             *** all the checks that meet the date criteria for the account
             GOSUB ONE.ID
          NEXT CDN

          *** Get the totals for all checks for the date range
          GOSUB PRT.GTOTALS

          PRINTER.OFF DOC.ID
          UT.PH.CLEANUP

          *** Send a message to user that the report is done
          SEND.MESSAGE 'Phantom',USER.ID,TITLE:' is complete'

          STOP
*-------------------------------------------------------------------------*
ONE.ID:   *** Get all the data for the check from the list created with
          *** all the checks that meet the date criteria for the account
          CD.ID = CD.IDS<CDN>
          TOL.CHECK  = 0
          TOL.GROSS  = 0
          TOL.DEDUCT = 0
          TOL.DISC   = 0
          FIRST.LN   = YES

          AR.MATREAD CD.ID,ASUB.ID
          IF FILTER.FLAG THEN
             * Filter off payable record
             PAY.ID = AR(2)<1,2>
             IF PAY.ID THEN
                AID  = FIELD(PAY.ID,'.',2)
                INVN = FIELD(PAY.ID,'.',3)+0
                MATREAD LED FROM LEDFILE,AID ELSE MAT LED = ''
                LOCATE INVN IN LED(8)<1> SETTING GEN ELSE GEN = 1
                FILTER.SELECT SKIP.FLAG,AID,GEN,FITEM
                IF SKIP.FLAG THEN RETURN
             END ELSE
                RETURN;* if no payable skip this id
             END
          END

          OID  = FIELD(CD.ID,'.',1)
          INVN = FIELD(CD.ID,'.',2)+0
          MATREAD LED FROM LEDFILE,OID   ELSE MAT LED = ''
          LOCATE INVN IN LED(8)<1> SETTING GEN ELSE GEN = 1
          CD.BR  = LED(2)<1,GEN,1>
          LOCATE CD.BR IN BRCHS<1> SETTING POS ELSE RETURN
          IF LED(6)<1,GEN>='X' AND NOT(VOID) THEN RETURN

          IF CURR.FLG THEN
             CONV.CURR.LED TGT,GEN
             CONV.CURR.AR TGT
          END

          IF LED(6)<1,GEN>='X' THEN LED(26) = ''

          IF GTYP[1,1] # 'B' THEN
             IF GTYP[1,1] # LED(27)<1,GEN> THEN RETURN
          END

          VEN = LED(1)<1,GEN>

          READV VEN.NAME FROM CUSFILE,LED(1)<1,GEN>,1 ELSE VEN.NAME = ''

          * If Vendor type is generic then look for generic vendor name
          * override on the payable ledger record and use it.
          GEN.VDR.PAY.IDS = AR(2)
          PAY.CNT = DCOUNT(GEN.VDR.PAY.IDS<1>,VM)
          FOR PAYNO = 2 TO PAY.CNT
             GEN.OID = FIELD(OCONV(GEN.VDR.PAY.IDS<1,PAYNO>,"G1.2"),".",1)
             READV GEN.VDR.INFO FROM LEDFILE,GEN.OID,78 ELSE GEN.VDR.INFO=""
             IF GEN.VDR.INFO<1,1> # "" THEN
                VEN.NAME = "*":GEN.VDR.INFO<1,1>
                EXIT
             END ELSE
                CONTINUE
             END
          NEXT PAYNO

          IF SORTOPT = 'V' AND LAST.VEN # VEN THEN GOSUB PRT.VTOTALS

          BNK.ID = CD.BR:'~':BANK.ID
          LOCATE BNK.ID IN LED(25)<1,GEN> SETTING POS THEN
             CHK.AMT = -LED(26)<1,GEN,POS>
          END ELSE
             CHK.AMT = 0
          END

          IF NOT(CURR.FLG) THEN
             IF LED(92)<1,GEN,2> THEN XRATE = OCONV(LED(92)<1,GEN,2>,'MR4') ELSE XRATE = 1
             XCURR   = LED(92)<1,GEN,1>
             CHK.AMT = ICONV(CHK.AMT/XRATE,'MR0')
          END

          CHK.NO   = FIELD(LED(13)<1,GEN>,'~',2)
          CHK.DATE = LED(23)<1,GEN>

          *** For E and Z type Check numbers show them correctly.
          PRE = CHK.NO[1,1]
          IF NUM(PRE) THEN
             DCHK.NO = CHK.NO   "R%8"
          END ELSE
             SUF.CHK = CHK.NO[2,99]
             IF NUM(SUF.CHK) THEN
                DCHK.NO = PRE:SUF.CHK "R%7"
             END ELSE
                DCHK.NO = (PRE:SUF.CHK) "R#8"
             END
          END

          PRINT DCHK.NO                         :' ':
          PRINT CD.BR                      "R#5":'   ':
          *PRINT SF.VN.NAME                 "L#27":' ':
          *PRINT VEN.NAME                   "L#27":' ':

          PRINT OCONV(CHK.DATE,'D2/')      "L#8":
          PRINT CHK.AMT                    "R26#12":XCURR"L#2":' ':

          IF LED(6)<1,GEN> = 'X' THEN
             PRINT '  **** Voided ****'
             IF OPT = "Detail" THEN PRINT
             RETURN
          END

          REV.ID   = ''   ;* Flag to indicate curr. looking at reversal ID
          REVERSAL = ''   ;* Flg to ind looking at a check that has been
                          ;* reversed
          *** If check has been reversed display '*** Reversed *** ' after
          *** the check amount.
          CHCKX.ID = BANK.ID:'~':CHK.NO
          READV DISB.REC FROM CHKXFILE,CHCKX.ID,1 THEN
             *** Check if check has been reversed, if a disbursement rec
             *** exist in the second VM in field one of the CHECK.XREF rec
             *** then check has been reversed
             IF DISB.REC<1,2>[1,1] = 'D' THEN
                *** Set REVERSAL flag to indicate D rec currently looking
                *** at is of a check that has been reversed
                REVERSAL = 1
                ORG.CHK = FIELD(DISB.REC<1,1>,'.',1)  ;* Orig Chk Disb ID
                *** Get the post date of the original check
                READV OP.DT FROM LEDFILE,ORG.CHK,23 ELSE OP.DT = ''
                *** Get the post date of the reversed check
                READV RP.DT FROM LEDFILE,FIELD(DISB.REC<1,2>,'.',1),23 ELSE RP.DT = ''
                *** If CD ID currently looking at is the same as
                *** CHECK.XREF<1,2>, then currently looking at the reversal
                *** Set REV.ID to indicate looking at reversal
                IF CD.ID = DISB.REC<1,2> THEN REV.ID = 1
                IF REV.ID THEN
                   *** If looking at reversal ID, display the post date of
                   *** the orig check
                   REV.MSG = 'Orig Chk Post Date    ':OCONV(OP.DT,'D2/')
                END ELSE
                   *** If looking at orig check, display the post date of
                   *** the reversed check
                   REV.MSG = ' Rev Chk Post Date    ':OCONV(RP.DT,'D2/')
                END
             END
          END

          IF OPT = "Summary" THEN PRINT

          *** For foreign exchange this is the only way to get a true total
          *** disc
          LOCATE 'DISCT' IN LED(24)<1,GEN> SETTING POS THEN
             TDISC.AMT = -LED(26)<1,GEN,POS>
          END ELSE TDISC.AMT = 0

          *** AR(2) contains a VM delimited list of all the payables that
          *** the check paid, point to the Disb. ID currently checking.
          *** When a check is reversed, the list of payables, AR(2), is
          *** lost and the disbursement ID of the reversal is stored.
          *** In order to display the gross amt, deductions, and
          *** discounts of a reversed check, we need to somehow get the
          *** payable IDs that were stored in AR(2) before it was reversed,
          *** so the gross amt, deduction, and discounts can then be read
          *** from AR(21) of the payable AR rec.  Now when a check is
          ***  reversed AR(2) containing the Payable IDs and AR(21) of the
          *** Payable ID are now stored in AR(35) of the disbursement ID.
          IF REVERSAL THEN
             PAY.IDS  = ''     ;* List containing the Payable IDs
             REV.AR21 = ''     ;* List containing AR(21) of the Payable ID
             *** For a reversed check, AR(35) contains the payable IDs
             *** instead of AR(2). In a reversed check AR(2) contains the
             *** disbursement ID of the reversal.
             IF AR(18) = 'CREV' THEN
                * For New reversal logic the payable is xref on both the
                * orginal check and the reversed check.
                PAY.IDS  = AR(2)
             END ELSE
                AR35.CT = DCOUNT(AR(35),VM)
                FOR AR35 = 2 TO AR35.CT
                   ONE.AR35 = AR(35)<1,AR35>
                   ONE.AR35 = RAISE(ONE.AR35)
                   PAY.IDS<1,AR35>  = ONE.AR35<1,1,1>
                   REV.AR21<1,AR35> = FIELD(ONE.AR35,SVM,2,99)
                NEXT AR35
                REV.AR21 = RAISE(REV.AR21)
             END
          END ELSE
             PAY.IDS  = AR(2)
          END
          PAY.DTS  = AR(22)
          PAY.AMTS = AR(5)
          PAY.CT   = DCOUNT(PAY.IDS,VM)
          FOR PAYN = 2 TO PAY.CT
             ID     = OCONV(PAY.IDS<1,PAYN>,'G1.2')
             AR.MATREAD ID,ASUB.ID
             OID    = FIELD(ID,'.',1)
             INVN   = FIELD(ID,'.',2)+0
             PAY.ON = PAY.DTS<1,PAYN>
             MATREAD LED FROM LEDFILE,OID ELSE MAT LED = ''
             LOCATE INVN IN LED(8)<1> SETTING GEN ELSE GEN = 1

             INV.NO   = LED(13)<1,GEN>
             SF.VN    = LED(5)<1,GEN>
             SF.VN.NAME = TRANS('ENTITY',SF.VN,1,'X')<1,1,1>
             INV.DATE = LED(10)<1,GEN>
             IF REVERSAL THEN
                CIDS = REV.AR21<PAYN,4>
             END ELSE
                CIDS  = AR(21)<1,4>
             END
             CCT   = DCOUNT(CIDS,SVM)
             FOR POS  = 1 TO CCT
                IF CD.ID = CIDS<1,1,POS> AND NOT(REVERSAL) THEN
                   PAY.AMT    = AR(21)<1,1,POS>
                   DISC.AMT   = AR(21)<1,2,POS>
                   DEDUCT.AMT = AR(21)<1,3,POS>
                   GROSS.AMT  = PAY.AMT + DISC.AMT + DEDUCT.AMT
                   *** Print the inv no, inv date, gross amt, deduct amt
                   *** and disc amount of the check
                   GOSUB PRT.AMT
                END ELSE
                   IF REVERSAL THEN
                      IF REV.ID THEN
                         REV.SIGN = -1
                      END ELSE
                         REV.SIGN = 1
                      END
                      PAY.AMT    = REV.AR21<PAYN,1,POS> * REV.SIGN
                      DISC.AMT   = REV.AR21<PAYN,2,POS> * REV.SIGN
                      DEDUCT.AMT = REV.AR21<PAYN,3,POS> * REV.SIGN
                      GROSS.AMT  = PAY.AMT + DISC.AMT + DEDUCT.AMT
                      *** Print the inv no, inv date, gross amt, deduct amt
                      *** and disc amount of the check
                      GOSUB PRT.AMT
                   END
                END
             NEXT POS
          NEXT PAYN

          IF OPT = "Detail" THEN
             IF FIRST.LN THEN PRINT
             IF REVERSAL THEN
                PRINT '  **** Reversed ****   ':REV.MSG:
                PRINT SPACE(13):'Check #':DCHK.NO:' Total  --- ':
             END ELSE
                PRINT SPACE(66):'Check #':DCHK.NO:' Total  --- ':
             END
             PRINT TOL.GROSS               "R26#12":
             PRINT TOL.DEDUCT              "R26#12":
             PRINT TOL.DISC                "R26#12"
             PRINT
          END

          IF SORTOPT='V' THEN
             VTOL.CHECK  += CHK.AMT
             VTOL.GROSS  += TOL.GROSS
             VTOL.DEDUCT += TOL.DEDUCT
             VTOL.DISC   += TOL.DISC
             VTOL.CT     += 1
          END

          GTOL.CHECK  += CHK.AMT
          GTOL.GROSS  += TOL.GROSS
          GTOL.DEDUCT += TOL.DEDUCT
          GTOL.DISC   += TOL.DISC

          RETURN
*-------------------------------------------------------------------------*
PRT.AMT:  *** Print the inv no, inv date, gross amt, deduct amt, and disc
          *** amount of the check
          IF OPT = "Detail" THEN
             IF NOT(FIRST.LN) THEN
                PRINT SPACE(40):
             END ELSE
                FIRST.LN = NO
             END
             PRINT SF.VN.NAME                 "L#27":'  ':
             PRINT INV.NO                     "L#15":'  ':
             PRINT OCONV(INV.DATE,'D2/')      "L#8":
             PRINT GROSS.AMT                  "R26#12":
             PRINT DEDUCT.AMT                 "R26#12":
             PRINT DISC.AMT                   "R26#12"
          END

          TOL.GROSS  += GROSS.AMT
          TOL.DEDUCT += DEDUCT.AMT
          TOL.DISC   += DISC.AMT

          RETURN
*-------------------------------------------------------------------------*
PRT.VTOTALS: *
          IF LAST.VEN#'@@@' THEN
             IF VTOL.CT>1 THEN
                PRINT SPACE(35):'Vendor Totals --- ':
                PRINT VTOL.CHECK              "R26#12":
                PRINT SPACE(28):
                IF OPT = "Detail" THEN
                   PRINT VTOL.GROSS              "R26#12":
                   PRINT VTOL.DEDUCT             "R26#12":
                   PRINT VTOL.DISC               "R26#12"
                   END
                END
             PRINT
             END

          LAST.VEN    = VEN
          VTOL.CHECK  = 0
          VTOL.GROSS  = 0
          VTOL.DEDUCT = 0
          VTOL.DISC   = 0
          VTOL.CT     = 0

          RETURN
*-------------------------------------------------------------------------*
PRT.GTOTALS: *
          GOSUB PRT.VTOTALS
          PRINT SPACE(36):'Grand Totals --- ':
          PRINT GTOL.CHECK                 "R26#12":
          PRINT SPACE(28):
          IF OPT = "Detail" THEN
             PRINT GTOL.GROSS                 "R26#12":
             PRINT GTOL.DEDUCT                "R26#12":
             PRINT GTOL.DISC                  "R26#12"
             END
          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *** Select all checks for the account entered by the user that
          *** meet the date criteria user entered
          CD.IDS  = ''
          SORTBYS = ''

          *** Specify the sort selection, this is determined by the user
          *** either sort by vendor or check #
          IF SORTOPT= 'C' THEN
             SORTBY = ' BY CHECK.NO'
          END ELSE
             SORTBY = ' BY SORTBY BY VN BY CHECK.NO'
          END

          *** Select statement to search in the CHECK.XREF file and create
          *** a list of all recs whose post dates are within the dates
          *** entered by the user.
          STMT=   'SSELECT CHECK.XREF ':SORTBY:' WITH BANK.ID = "':BANK.ID:'" AND WITH POST.DATE >= "':SD:'" AND WITH POST.DATE <= "':ED:'"'
          EXECUTE STMT CAPTURING MSG

          LOOP
             READNEXT XID ELSE EXIT
             READV ID FROM CHKXFILE,XID,1 ELSE ID = ''
             *** Check the post date of each of the disbursement rec to
             *** make sure they each fall in the date criteria, for
             ***  example, both the orig check and the reversal if reversed
             ID.CT =  DCOUNT(ID,VM)
             FOR ICT = 1 TO ID.CT
                O.ID = ID<1,ICT>
                O.ID = FIELD(O.ID,'.',1)
                READV P.DATE FROM LEDFILE,O.ID,23 ELSE P.DATE = ''
                IF P.DATE >= ICONV(SD,"D") AND P.DATE <= ICONV(ED,"D") THEN
                   CD.IDS<-1> = ID<1,ICT>
                END
             NEXT ICT
          REPEAT

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~01/18/17~15:12
